package com.itheima.tilas.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.dto.SelectClazzPageDTO;
import com.itheima.tilas.entity.Clazz;
import com.itheima.tilas.mapper.ClazzMapper;
import com.itheima.tilas.po.InsertClazzOneParam;
import com.itheima.tilas.po.SelectClazzPageParam;
import com.itheima.tilas.po.UpdateClazzParam;
import com.itheima.tilas.service.ClazzService;
import com.itheima.tilas.vo.PageVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 班级类服务层实现类
 *
 * @author 132
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    @Resource
    ClazzMapper clazzMapper;

    @Override
    public ResultUtil selectClazzPage(SelectClazzPageParam param) {
        Page<Object> objects = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectClazzPageDTO> clazz = clazzMapper.selectClazzPage(param);
        clazz.forEach(o -> {
            if (o.getBeginDate().isAfter(LocalDate.now())) {
                o.setStatus("未开班");
            } else if (o.getEndDate().isAfter(LocalDate.now())) {
                o.setStatus("已开班");
            } else {
                o.setStatus("已结课");
            }
        });
        PageVo<SelectClazzPageDTO> pageVo = new PageVo<>();
        pageVo.setTotal(objects.getTotal());
        pageVo.setRows(clazz);
        return ResultUtil.success(pageVo);
    }

    @Override
    public int deleteClazzById(Integer id) {
        return clazzMapper.deleteClazzById(id);
    }

    @Override
    public int insertClazzOne(InsertClazzOneParam param) {
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        return clazzMapper.insertClazzOne(param);
    }

    @Override
    public SelectClazzPageDTO selectClazzById(Integer id) {
        return clazzMapper.selectClazzById(id);
    }

    @Override
    public int updateClazzById(UpdateClazzParam param) {
        //TODO添加班级名字和教室是否唯一的判断
        param.setUpdateTime(LocalDateTime.now());
        return clazzMapper.updateClazzById(param);
    }

    @Override
    public List<Clazz> selectClazzAll() {
        return clazzMapper.selectClazzAll();
    }
}




























